package com.zhihao.takeaway.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhihao.takeaway.common.utils.DateTool;
import com.zhihao.takeaway.common.utils.EasyUITable;
import com.zhihao.takeaway.common.utils.JwtUtil;
import com.zhihao.takeaway.common.utils.Result;
import com.zhihao.takeaway.entity.Marketer;
import com.zhihao.takeaway.service.MarketerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;


@RestController
@RequestMapping("/marketer")
public class MarketerController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private MarketerService marketerService;

    @PostMapping("/getMarketerPageData")
    public Result getPageData(Integer nownum,Integer wantnum,String input)throws Exception{
        String id = getId();
        Result result = new Result();
        if(id == null || id.equals("")){
            result.fail("当前用户未登录");
        }else{
            String lockKey = "marketer";
            EasyUITable easyUITable = marketerService.getPageData(nownum,wantnum,input);
            result.success("查询成功");
            result.setData(easyUITable);
        }
        return result;
    }
    @PostMapping("/getDataInLayout")
    public Result getDataInLayout(Integer currentPage,Integer wantnum,String input){
        String id = getId();
        Result result = new Result();
        if(id == null || id.equals("")){
            result.fail("当前用户未登录");
        }else{
            EasyUITable easyUITable = marketerService.getMoreData(currentPage,wantnum,input);
            result.setData(easyUITable);
            result.success("查找成功");
        }
        return result;
    }
    @PostMapping("/add")
    public Result add(@RequestBody Marketer marketer){
        String id = getId();
        Result result = new Result();
        if(id == null || id.equals("")){
            result.fail("当前用户未登录");
        }else{
            marketer.setUpdateTime(DateTool.getCurrTime());
            marketer.setCreateTime(DateTool.getCurrTime());
            QueryWrapper<Marketer>queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Marketer::getStoreName,marketer.getStoreName());
            queryWrapper.lambda().eq(Marketer::getName,marketer.getName());
            queryWrapper.lambda().eq(Marketer::getIdNumber, marketer.getIdNumber());
            queryWrapper.lambda().eq(Marketer::getCityName,marketer.getCityName());
            queryWrapper.lambda().eq(Marketer::getDistrictName,marketer.getDistrictName());
            queryWrapper.lambda().eq(Marketer::getDetail,marketer.getDetail());
            Marketer marketer1 = marketerService.getOne(queryWrapper);
            if(marketer1 == null){
                String uid = UUID.randomUUID().toString();
                marketer.setId(uid);
                marketerService.save(marketer);
                result.success("成功添加");
            }else{
                result.fail("添加失败，当前商铺已被注册");
            }
        }
        return result;
    }
    @PostMapping("/update")
    public Result update(@RequestBody Marketer marketer){
        String id = getId();
        Result result = new Result();
        if(id == null || id.equals("")){
            result.fail("当前用户未登录");
        }else{
            marketer.setUpdateTime(DateTool.getCurrTime());
            marketerService.updateById(marketer);
            result.success("更新成功");
        }
        return result;
    }
    @PostMapping("/delete")
    public Result delete(@RequestBody Marketer marketer){
        String id = getId();
        Result result = new Result();
        if(id == null || id.equals("")){
            result.fail("当前用户未登录");
        }else{
            marketerService.removeById(marketer.getId());
            result.success("删除成功");
        }
        return result;
    }
    public String getId(){
        String token = request.getHeader("token");
        String id = JwtUtil.validateToken(token);
        return id;
    }
}
